/*
 * ClientEntry.cpp
 *
 *  Created on: 28.03.2011
 *      Author: niea
 */

#include "ClientEntry.hpp"
#include "Utils.hpp"
#include "Settings.hpp"
#include <vector>

using namespace efs;
using namespace rapidxml;

efs::ClientEntry::ClientEntry()
{
}

efs::ClientEntry::~ClientEntry()
{
}

bool efs::ClientEntry::operator==(ClientEntry const &entry) const
{
	if (ip == entry.ip && tcp_port == entry.tcp_port && file_port == entry.file_port)
	{
		return true;
	}
	else
	{
		return false;
	}
}


bool efs::ClientEntry::putXMLString(std::string xml_string)
{
	//pantheios::log_DEBUG("xml_string : " + xml_string);
	try
	{
		std::vector<char> xml_vector(xml_string.begin(), xml_string.end());
		xml_vector.push_back('\0');
		xml_document<> xml;
		xml.parse<rapidxml::parse_declaration_node | rapidxml::parse_no_data_nodes>(&xml_vector[0]);
		xml_node<> *root = xml.first_node("root");
		root = root->first_node("connection");
		xml_node<> *node_port = root->first_node("tcp-port");
		xml_node<> *node_file_port = root->first_node("file-port");
		xml_node<> *node_ip = root->first_node("ip");

		tcp_port = strToInt(node_port->first_attribute("tcp-port")->value());
		file_port = strToInt(node_file_port->first_attribute("file-port")->value());
		ip = node_ip->first_attribute("ip")->value();
	}
	catch(...)
	{
		pantheios::log_ERROR("[ClientEntry]: error parsing xml string");
		return false;
	}
	return true;
}


std::string efs::ClientEntry::getXMLString() const
{
	rapidxml::xml_document<> xml;
	xml_node<>* decl = xml.allocate_node(node_declaration);
	decl->append_attribute(xml.allocate_attribute("version", "1.0"));
	decl->append_attribute(xml.allocate_attribute("encoding", "utf-8"));
	xml.append_node(decl);

	xml_node<>* root = xml.allocate_node(node_element, "root");
	xml_node<>* connection = xml.allocate_node(node_element, "connection");
	root->append_node(connection);
	xml.append_node(root);
	root = connection;

	xml_node<>* info = xml.allocate_node(node_element, "ip");
	info->append_attribute(xml.allocate_attribute("ip",  xml.allocate_string(ip.c_str(), ip.length() + 1) ));
	root->append_node(info);

	std::string s_tcp_port = intToStr(tcp_port);
	info = xml.allocate_node(node_element, "tcp-port");
	info->append_attribute(xml.allocate_attribute("tcp-port",  xml.allocate_string(s_tcp_port.c_str(), s_tcp_port.length() + 1) ));
	root->append_node(info);

	std::string s_file_port = intToStr(file_port);
	info = xml.allocate_node(node_element, "file-port");
	info->append_attribute(xml.allocate_attribute("file-port",  xml.allocate_string(s_file_port.c_str(), s_file_port.length() + 1) ));
	root->append_node(info);

	std::string xml_string;
	rapidxml::print(std::back_inserter(xml_string), xml);
	return xml_string;
}

void efs::ClientEntry::appendToExternalXml(rapidxml::xml_document<> &xml, rapidxml::xml_node<> *root)
{
	xml_node<>* connection = xml.allocate_node(node_element, "connection");
	root->append_node(connection);

	//pantheios::log_DEBUG("[ClientEntry]: appending to external xml");
	xml_node<>* info = xml.allocate_node(node_element, "ip");
	info->append_attribute(xml.allocate_attribute("ip",  xml.allocate_string(ip.c_str(), ip.length() + 1) ));
	connection->append_node(info);

	std::string s_tcp_port = intToStr(tcp_port);
	info = xml.allocate_node(node_element, "tcp-port");
	info->append_attribute(xml.allocate_attribute("tcp-port",  xml.allocate_string(s_tcp_port.c_str(), s_tcp_port.length() + 1) ));
	connection->append_node(info);

	std::string s_file_port = intToStr(file_port);
	info = xml.allocate_node(node_element, "file-port");
	info->append_attribute(xml.allocate_attribute("file-port",  xml.allocate_string(s_file_port.c_str(), s_file_port.length() + 1) ));
	connection->append_node(info);
	//pantheios::log_DEBUG("[ClientEntry]: appending to external xml - OK");
}


void efs::ClientEntry::print() const
{
	pantheios::log_DEBUG("[Client entry]: IP=" + ip + " tcp-port=" + intToStr(tcp_port)
			 + " file-port=" + intToStr(file_port));
}
